1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| import requests import json import os import threading
API_KEY = 'xxxxxxxxx'
MODEL = 'text-davinci-003'
BOT_TOKEN = 'xxxxxxxxxxxxx'
BOT_PERSONALITY = ''
def openAI(prompt): response = requests.post( 'https://api.openai.com/v1/completions', headers={'Authorization': f'Bearer {API_KEY}'}, json={'model': MODEL, 'prompt': prompt, 'temperature': 0.4, 'max_tokens': 4000} ) result = response.json() print(result) final_result = ''.join(choice['text'] for choice in result['choices']) return final_result
def openAImage(prompt): resp = requests.post( 'https://api.openai.com/v1/images/generations', headers={'Authorization': f'Bearer {API_KEY}'}, json={'prompt': prompt, 'n': 1, 'size': '1024x1024'} ) response_text = json.loads(resp.text) return response_text['data'][0]['url']
def telegram_bot_sendtext(bot_message, chat_id, msg_id): data = { 'chat_id': chat_id, 'text': bot_message, 'reply_to_message_id': msg_id } response = requests.post( 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage', json=data ) return response.json()
def telegram_bot_sendimage(image_url, group_id, msg_id): data = { 'chat_id': group_id, 'photo': image_url, 'reply_to_message_id': msg_id } url = 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendPhoto' response = requests.post(url, data=data) return response.json()
def Chatbot(): cwd = os.getcwd() filename = cwd + '/chatgpt.txt' if not os.path.exists(filename): with open(filename, "w") as f: f.write("1") else: print("File Exists") with open(filename) as f: last_update = f.read() url = f'https://api.telegram.org/bot{BOT_TOKEN}/getUpdates?offset={last_update}' response = requests.get(url) data = json.loads(response.content) for result in data['result']: try: if float(result['update_id']) > float(last_update): if not result['message']['from']['is_bot']: last_update = str(int(result['update_id'])) msg_id = str(int(result['message']['message_id'])) chat_id = str(result['message']['chat']['id']) if '/img' in result['message']['text']: prompt = result['message']['text'].replace("/img", "") bot_response = openAImage(prompt) print(telegram_bot_sendimage(bot_response, chat_id, msg_id)) if '@chatGpt_dandelion_bot' in result['message']['text'] \ or '/ask' in result['message']['text']: prompt = result['message']['text'].replace("@chatGpt_dandelion_bot", "") bot_response = openAI(f"{BOT_PERSONALITY}{prompt}") print(telegram_bot_sendtext(bot_response, chat_id, msg_id)) if 'reply_to_message' in result['message']: if result['message']['reply_to_message']['from']['is_bot']: prompt = result['message']['text'] bot_response = openAI(f"{BOT_PERSONALITY}{prompt}") print(telegram_bot_sendtext(bot_response, chat_id, msg_id)) except Exception as e: print(e) with open(filename, 'w') as f: f.write(last_update) return "done"
def main(): timertime = 5 Chatbot() threading.Timer(timertime, main).start()
if __name__ == "__main__": main()
|